<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Geometric Data Types</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"/><link rel="home" href="index.html" title="The PostgreSQL™ JDBC Interface"/><link rel="up" href="ext.html" title="Chapter 9. PostgreSQL™ Extensions to the JDBC API"/><link rel="prev" href="ext.html" title="Chapter 9. PostgreSQL™ Extensions to the JDBC API"/><link rel="next" href="largeobjects.html" title="Large Objects"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Geometric Data Types</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ext.html">Prev</a> </td><th width="60%" align="center">Chapter 9. <span class="productname">PostgreSQL</span>™ Extensions to the
    <acronym class="acronym">JDBC</acronym> <acronym class="acronym">API</acronym></th><td width="20%" align="right"> <a accesskey="n" href="largeobjects.html">Next</a></td></tr></table><hr/></div><div class="sect1" title="Geometric Data Types"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="geometric"/>Geometric Data Types</h2></div></div></div><p>
    <span class="productname">PostgreSQL</span>™ has a set of data types that
    can store geometric features into a table. These include single
    points, lines, and polygons.  We support these types in Java with
    the org.postgresql.geometric package.  Please consult the Javadoc
    for the details of available classes and features metioned in
    <a class="xref" href="reading.html" title="Chapter 12. Further Reading">Chapter 12, <i>Further Reading</i></a>.
   </p><div class="example"><a id="geometric-circle-example"/><p class="title"><b>Example 9.1. Using the circle datatype from <acronym class="acronym">JDBC</acronym></b></p><div class="example-contents"><pre class="programlisting">

import java.sql.*;

import org.postgresql.geometric.PGpoint;
import org.postgresql.geometric.PGcircle;

public class GeometricTest {

	public static void main(String args[]) throws Exception {
		Class.forName("org.postgresql.Driver");
		String url = "jdbc:postgresql://localhost:5432/test";

		Connection conn = DriverManager.getConnection(url,"test","");

		Statement stmt = conn.createStatement();
		stmt.execute("CREATE TEMP TABLE geomtest(mycirc circle)");
		stmt.close();

		insertCircle(conn);
		retrieveCircle(conn);
		conn.close();
	}

	private static void insertCircle(Connection conn) throws SQLException {

		PGpoint center = new PGpoint(1, 2.5);
		double radius = 4;
		PGcircle circle = new PGcircle(center, radius);

		PreparedStatement ps = conn.prepareStatement("INSERT INTO geomtest(mycirc) VALUES (?)");
		ps.setObject(1, circle);
		ps.executeUpdate();
		ps.close();
	}
	
	private static void retrieveCircle(Connection conn) throws SQLException {
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT mycirc, area(mycirc) FROM geomtest");
		rs.next();
		PGcircle circle = (PGcircle)rs.getObject(1);
		double area = rs.getDouble(2);

		PGpoint center = circle.center;
		double radius = circle.radius;

		System.out.println("Center (X, Y) = (" + center.x + ", " + center.y + ")");
		System.out.println("Radius = " + radius);
		System.out.println("Area = " + area);
	}
}
</pre></div></div><br class="example-break"/></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ext.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ext.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="largeobjects.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 9. <span class="productname">PostgreSQL</span>™ Extensions to the
    <acronym class="acronym">JDBC</acronym> <acronym class="acronym">API</acronym> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Large Objects</td></tr></table></div></body></html>